﻿#include "LineDataDealThread.h"
#include "GlobalHeader.h"

LineDataDealThread::LineDataDealThread(QObject *parent)
	: QThread(parent)
{
	m_MapIndex[0] = QPair<QString, QString>("date", "日期");
	m_MapIndex[1] = QPair<QString, QString>("hour", "时间");
	m_MapIndex[2] = QPair<QString, QString>("type", "类型");
	m_MapIndex[490] = QPair<QString, QString>("1490A", "监测站");
	m_MapIndex[491] = QPair<QString, QString>("1491A", "收费所");
	m_MapIndex[492] = QPair<QString, QString>("1492A", "新疆农科院农场(对照点)");
	m_MapIndex[493] = QPair<QString, QString>("1493A", "铁路局");
	m_MapIndex[494] = QPair<QString, QString>("1494A", "三十一中学");
	m_MapIndex[495] = QPair<QString, QString>("1495A", "七十四中学");
	m_MapIndex[496] = QPair<QString, QString>("1496A", "米东区环保局");
	m_MapIndex[1487] = QPair<QString, QString>("2703A", "市环保局");
	m_MapIndex[1488] = QPair<QString, QString>("2704A", "新政府片区");
	m_MapIndex[1489] = QPair<QString, QString>("2705A", "第二水厂");
}

LineDataDealThread::~LineDataDealThread()
{
	CloseAllMapFile();
}

void LineDataDealThread::run()
{
	gAddLog(LOGTYPE_MESSAGE, u8"开始数据处理线程");
	if (!g_pCSVFileReadThread)
	{
		gAddLog(LOGTYPE_ERROR, u8"文件读取线程未启用");
		return;
	}
	QString strLineData;
	//第一行获取索引
// 	while (!g_bExit && !isInterruptionRequested())
// 	{
// 		if (g_pCSVFileReadThread->GutLineData(strLineData))
// 		{
// 			QStringList DataList = strLineData.split(',');
// 			QList<QString> IndexList = m_MapIndex.keys();
// 			for (unsigned int uiIndex = 0; uiIndex < DataList.size(); uiIndex++)
// 			{
// 				const QString &strIndex = DataList.at(uiIndex);
// 				QMap<QString, QPair<unsigned int, QString>>::iterator itor = m_MapIndex.begin();
//				for (itor; itor != m_MapIndex.end(); itor++)
// 				{
// 					if (DataList.contains(itor.key()))
// 					{
// 						itor.value().first = uiIndex;
// 					}
// 				}
// 			}
// 			break;
// 		}
// 		else
// 		{
// 			//数据读取线程结束 数据处理完成 则停止
// 			if (g_pCSVFileReadThread->isFinished())
// 			break;
// 		}
// 	}
	QString strTitle = u8"date,hour,type,监测站(1490A),收费所(1491A) ,新疆农科院农场(对照点)(1492A),铁路局(1493A),三十一中学(1494A),七十四中学(1495A),米东区环保局(1496A),";
	strTitle += u8"市环保局(2703A),新政府片区(2704A),第二水厂(2705A)";
	//分文件
	HandleFileMap(strTitle);
	//获取到行数据写文件
//	QList<QPair<QString, QString>> IndexList = m_MapIndex.values();
	while (!g_bExit && !isInterruptionRequested())
	{
		do
		{
			QStringList DataList = strLineData.split(',');
			if (DataList.size() > 10)
			{
				QMap<unsigned int, QPair<QString, QString>>::iterator itor = m_MapIndex.begin();
				QString strLineData = "\n";
				for (itor; itor != m_MapIndex.end(); itor++)
				{
					unsigned int uiCurIndex = itor.key();
					if (DataList.size() > uiCurIndex)
					{
						//取到每一列的值写每一列
						strLineData += DataList.at(uiCurIndex);
						strLineData += ",";
						/************************************************************************/
						/* date hour type 监测站(1490A) 收费所(1491A)  新疆农科院农场(对照点)(1492A) 铁路局(1493A) 三十一中学(1494A)  七十四中学(1495A) 米东区环保局(1496A) */
						/************************************************************************/
					}
				}
				strLineData.resize(strLineData.size() - 1);
				QString strType = DataList.at(2);
				//判别类型
				if (m_MapFileIndex.find(strType) != m_MapFileIndex.end())
					m_MapFileIndex[strType]->write(strLineData.toLatin1());
				else
					gAddLog(LOGTYPE_WARNING, "未识别产品名称:" + strType);
			}
		} 
		while (GetLineData(strLineData) && !g_bExit && !isInterruptionRequested());
		QThread::msleep(20);
		//数据读取线程结束 数据处理完成 则停止
		if (!g_pCSVFileReadThread || g_pCSVFileReadThread->isFinished())
			break;
	}
	CloseAllMapFile();
	gAddLog(LOGTYPE_MESSAGE, u8"文件存储成功,目录:OutputData");
	gAddLog(LOGTYPE_MESSAGE, u8"数据处理线程退出");
}

bool LineDataDealThread::PutLineData(const QString &strLineData)
{
	m_ReadWriteLock.lockForWrite();
// 	if (m_QueueLineData.size() > 500)
// 		m_QueueLineData.removeFirst();
	m_QueueLineData.enqueue(strLineData);
	m_ReadWriteLock.unlock();
	return true;
}

bool LineDataDealThread::GetLineData(QString &strLineData)
{
	m_ReadWriteLock.lockForRead();
	bool bResult = false;
	if (m_QueueLineData.isEmpty())
		bResult = false;
	else
	{
		strLineData = m_QueueLineData.dequeue();
		bResult = true;
	}
	m_ReadWriteLock.unlock();
	return bResult;
}

//QFile运算符重载

void LineDataDealThread::HandleFileMap(const QString &strTitle)
{
	QString strFileDirName = u8"./OutputData/";
	QDir dir;
	if (!dir.exists(strFileDirName))
		dir.mkpath(strFileDirName);
	QString strFileName = "乌鲁木齐与伊犁_";
	strFileName += g_strDealPath.right(g_strDealPath.size() - 1 - g_strDealPath.lastIndexOf('/'));
	strFileName += QDateTime::currentDateTime().toString("yyyyMMddHHmmsszzz");
	strFileName += ".csv";

	QStringList List;
	List << "AQI" << "PM2.5" << "PM2.5_24h" << "PM10" << "PM10_24h" << "SO2" << "SO2_24h" << "NO2" << "NO2_24h" << "O3" << "O3_24h" << "O3_8h" << "O3_8h_24h" << "CO" << "CO_24h";
	unsigned char ucNum = List.size();
	for (unsigned char ucIndex = 0; ucIndex < ucNum; ucIndex++)
	{
		QString str = List.at(ucIndex);
		if (m_MapFileIndex.find(str) == m_MapFileIndex.end())
			m_MapFileIndex[str] = new QFile;
		m_MapFileIndex[str]->setFileName(strFileDirName + str + strFileName);
		if (!m_MapFileIndex[str]->open(QFile::Append))
		{
			gAddLog(LOGTYPE_WARNING, u8"文件打开失败:" + m_MapFileIndex[str]->fileName());
		}
		else
		{

			QTextCodec* utf8Codec = QTextCodec::codecForName("utf-8");
			QTextCodec* gb2312Codec = QTextCodec::codecForName("gb2312");

			QString strUnicode = utf8Codec->toUnicode(strTitle.toUtf8());        //无编码
			QByteArray gb2312 = gb2312Codec->fromUnicode(strUnicode);       //无编码转换

			m_MapFileIndex[str]->write(gb2312);
		}
	}
}

void LineDataDealThread::CloseAllMapFile()
{
	QMap<QString, QFile*>::iterator it = m_MapFileIndex.begin();
	while (it != m_MapFileIndex.end())
	{
		it.value()->close();
		it++;
	}
}